package com.demo.webapp.ui;

import java.util.Map;

import javax.servlet.ServletContext;

import com.demo.webapp.ui.displayer.Displayer;
import com.demo.webapp.ui.displayer.DisplayerMapping;
import com.demo.webapp.ui.displayer.DisplayerMappingImpl;
/**
 * 
 * 持久化资源实现类
 * @author ryuu.kk
 *
 */
//net.sf.navigator.menu.MenuRepository?
public class UIRepositoryResource implements RepositoryResource {

	protected String config;
	protected String name;
	protected ServletContext servletContext = null;
	private DisplayerMapping mapping = new DisplayerMappingImpl();
	
	private Map<String, Component> components = new java.util.concurrent.ConcurrentHashMap<String, Component>();

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getName() {
		return this.name;
	}
	
//	public void addComponent(ComponentBase model) {
//		components.put(model.getName(), model);
//	}

	@Override
	public ServletContext getServletContext() {
		return this.servletContext;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	@Override
	public Displayer<?> getDisplayerMapping(String name) {
		return mapping.get(name);
	}

	@Override
	public <T extends Component> void addComponent(T t) {
		components.put(t.getName(), t);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T getComponent(String name) {
		return (T) components.get(name);
	}

	public void addDisplayer(Displayer<?> displayer) {
		if (mapping == null) {
			mapping = new DisplayerMappingImpl();
		}
		mapping.put(displayer);
	}
	public void setMapping(DisplayerMapping mapping) {
		this.mapping = mapping;
	}
	@Override
	public DisplayerMapping getMapping() {
		return mapping;
	}
}
